#include "SoundSystem.h"

#pragma comment(lib, "irrKlang.lib")

SoundSystem::SoundSystem(void):SoundSystem_base()
{
	
	engine = ::createIrrKlangDevice();
	if (!engine)
		cout<<"Error initializing sound engine!!!"<<endl;
}

SoundSystem::~SoundSystem(void)
{
	engine->drop();
}

void SoundSystem::AddStream(char *FileName, float Volume = 1.0f , float Pan = 0, bool isStreaming = true, bool isRepeat = false)
{
	if (!engine)
		return;

	if (!isStreaming)
		GenericSound = engine->addSoundSourceFromFile(FileName,::ESM_NO_STREAMING,true);	
	else
		GenericSound = engine->addSoundSourceFromFile(FileName,::ESM_AUTO_DETECT,true);	
	PlayingSound = engine->play2D(GenericSound,isRepeat,true,false,false);
	PlayingSound->setVolume(Volume);
	PlayingSound->setPan(Pan);
}

ISound* SoundSystem::AddStream(char *FileName, float Volume , float Pan, bool isRepeat)
{
	if (!engine)
		return 0;
	
	GenericSound = engine->addSoundSourceFromFile(FileName,::ESM_AUTO_DETECT,true);	
	PlayingSound = engine->play2D(GenericSound,isRepeat,true,false,false);
	PlayingSound->setVolume(Volume);
	PlayingSound->setPan(Pan);
	return PlayingSound;
}

ISound* SoundSystem::Play3DSound(char *FileName, float Volume, bool isStreaming, bool isRepeat, irrklang::vec3df position, bool track, bool SoundEffect)
{
	ISound* Sound;
	if (!engine)
		return 0;
	if (!isStreaming)
		Sound = engine->play3D(FileName,position,isRepeat,true,track,::ESM_NO_STREAMING,SoundEffect);
	else
		Sound = engine->play3D(FileName,position,isRepeat,true,track,::ESM_AUTO_DETECT,SoundEffect);
	if (Sound)
		Sound->setVolume(Volume);
	return Sound;
}

void SoundSystem::SetListenerPos(vec3df pos, vec3df lookdir, vec3df velPerSec, vec3df UpVec)
{
	engine->setListenerPosition(pos,lookdir,velPerSec,UpVec);
}


void SoundSystem::PlayMusic(ISound* TempSound)
{
	if (!engine)
		return;
	if (TempSound)
		if(TempSound->getIsPaused())
			TempSound->setIsPaused(false);
}

void SoundSystem::PlayMusic(void)
{
	if (!engine)
		return;
	if (PlayingSound)
		if(PlayingSound->getIsPaused())
			PlayingSound->setIsPaused(false);
}

void SoundSystem::StopMusic(ISound* TempSound)
{
	if (!engine)
		return;
	if (TempSound)
		if(!TempSound->getIsPaused())
			TempSound->setIsPaused(true);
}

void SoundSystem::StopMusic(void)
{
	if (!engine)
		return;
	if (PlayingSound)
		if(!PlayingSound->getIsPaused())
			PlayingSound->setIsPaused(true);
}


void SoundSystem::ResetMusic(ISound* TempSound)
{
	if (!engine)
		return;
	if (TempSound)
	{
		if (!TempSound->getIsPaused())
			TempSound->setIsPaused(true);
		TempSound->setPlayPosition(0);
	}
}

void SoundSystem::ResetMusic(void)
{
	if (!engine)
		return;
	if (PlayingSound)
		if (PlayingSound->getIsPaused())
			PlayingSound->setPlayPosition(0);
}

void SoundSystem::FastPlayFX(char *FileName, float volume=1.0f)
{
	if (engine)
	{
		ISound* Sound = engine->play2D(FileName);
		if (Sound)
			Sound->setVolume(volume);
	}
}